package org.wanho.servlet;

import com.alibaba.fastjson.JSON;
import org.wanho.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2023-05-04 13:45
 * Description：<描述>
 */
@WebServlet("/jsonp")
public class JsonpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String callback = req.getParameter("callback"); // handle

        resp.setContentType("application/json;charset=utf8");
        PrintWriter out = resp.getWriter();

        User user = new User(101,"徐嵩",20,new Date());
        out.print(callback+"("+JSON.toJSONString(user)+")");  // handle(data)
        out.close();
    }
}
